var express = require('express');
var router = express.Router();
var myPool = require('../pool/poolmongodb').myPool;

// 获取一级菜单
router.get('/oneMenu', async (req, res, next) => {
    // 查找第一级菜单
    try {
        // 查找第一级菜单
        var client = await myPool.acquire();
        let cursor = client.db('mydatabase').collection('origin_menu').find({
            cat_pid: 0
        });
        //下面使用的就是常规操作啦，因为主要讲的连接池，就懒得写了...
        let result = await cursor.toArray();
        //响应查询的结果
        res.json({
            status: 1,
            data: result
        });
    } catch (error) {
        res.json({
            status: 0,
            message: "错误"
        });
    }
});

// 获取二级菜单
router.get('/twoMenu', async (req, res, next) => {
    try {
        // 查找第一级菜单
        var client = await myPool.acquire();
        let cursor = client.db('mydatabase').collection('origin_menu').find({
            cat_pid: 0
        });
        let result = await cursor.toArray();
        for (let i = 0; i < result.length; i++) {
            // 查找二级菜单
            let cursor_two = client.db('mydatabase').collection('origin_menu').find({
                cat_pid: result[i].cat_id
            });
            let two_level = await cursor_two.toArray();
            result[i].children = two_level;
        }
        res.json({
            status: 1,
            data: result
        });
    } catch (error) {
        res.json({
            status: 0,
            message: "错误"
        });
    }

});
// 获取三级菜单
router.get('/threeMenu', async (req, res, next) => {
    try {
        // 查找第一级菜单
        var client = await myPool.acquire();
        let cursor = client.db('mydatabase').collection('origin_menu').find({
            cat_pid: 0
        });
        let result = await cursor.toArray();
        for (let i = 0; i < result.length; i++) {
            // 查找二级菜单
            let cursor_two = client.db('mydatabase').collection('origin_menu').find({
                cat_pid: result[i].cat_id
            });
            let two_level = await cursor_two.toArray();
            result[i].children = two_level;
            for (let j = 0; j < result[i].children.length; j++) {
                // 查找三级菜单
                let cursor_three = client.db('mydatabase').collection('origin_menu').find({
                    cat_pid: result[i].children[j].cat_id
                });
                let three_level = await cursor_three.toArray();
                result[i].children[j].children = three_level;
            }
        }
        res.json({
          status: 1,
          data: result
        });
      } catch (error) {
        res.json({
          status: 0,
          message: "错误"
        });
      }
});
// 原始数据
router.get('/originMenu', async (req, res, next) => {
    try {
        var client = await myPool.acquire();
        let cursor = client.db('mydatabase').collection('origin_menu').find();
        //下面使用的就是常规操作啦，因为主要讲的连接池，就懒得写了...
        let result = await cursor.toArray();
        //响应查询的结果
        res.json({
            status: 1,
            data: result
        });
        // 释放连接
        myPool.release(client);
    } catch (error) {
        res.json({
            status: 0,
            message: "错误"
        });
    }
});
// 原始已分层级的数据
router.get('/getMenu', async (req, res, next) => {
    try {
        var client = await myPool.acquire();
        let cursor = client.db('mydatabase').collection('sp_category').find();
        //下面使用的就是常规操作啦，因为主要讲的连接池，就懒得写了...
        let result = await cursor.toArray();
        //响应查询的结果
        res.json({
            status: 1,
            data: result
        });
        // 释放连接
        myPool.release(client);
    } catch (error) {
        res.json({
            status: 0,
            message: "错误"
        });
    }
});
module.exports = router;